package cn.mado.org.service.impl;

import cn.mado.basic.util.PageList;
import cn.mado.org.domain.Dept;
import cn.mado.org.mapper.DeptMapper;
import cn.mado.org.query.DeptQuery;
import cn.mado.org.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    @Override
    @Transactional
    public void add(Dept dept) {
        dept.setCreationTime(new Date());
        dept.setFiction(0L);
        dept.setMovie(0L);
        dept.setWriter(0L);
        deptMapper.add(dept);
    }

    @Override
    @Transactional
    public void delete(Long deptId) {
        deptMapper.delete(deptId);
    }

    @Override
    @Transactional
    public void update(Dept dept) {
        deptMapper.update(dept);
    }

    @Override
    public Dept findById(Long deptId) {
        return deptMapper.findById(deptId);
    }

    @Override
    public PageList<Dept> queryByPage(DeptQuery query) {
        // 查询条数
        Integer totals = deptMapper.queryByCount(query);
        if (null == totals || 0 == totals){
            return new PageList<>(0,new ArrayList<>());
        }
        // 查询数据
        List<Dept> depts = deptMapper.queryByPage(query);
        System.out.println(depts);
        //方到返回里面
        return new PageList<>(totals,depts);
    }

    //查询一二级
    @Override
    public List<Dept> findTree() {
        return deptMapper.findOneAndTwolevelTree();
    }
}
